package cn.bytecollege.user.controller;

import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.OrderDetail;
import cn.bytecollege.common.entity.Product;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.user.service.OrderDetailService;

import javax.lang.model.element.NestingKind;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "OrderDetailSaveController",value = "/user/orderdetailsave")
public class OrderDetailSaveController extends HttpServlet {
    private OrderDetailService orderDetailService=null;
    private ProductService productService=null;

    public OrderDetailSaveController(){
        orderDetailService=new OrderDetailService();
        productService=new ProductService();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderDetailId=request.getParameter("orderDetailId");
        OrderDetail orderDetail=orderDetailService.findById(Integer.valueOf(orderDetailId));
      if(orderDetail.getProductNumber()==null){
          orderDetail.setProductNumber(1);
      }
      HttpSession session= request.getSession();
      User user=(User) session.getAttribute("user");
      Integer userId=user.getUserId();
      orderDetail.setUserId(userId);
      orderDetailService.save(orderDetail);
      request.getRequestDispatcher("/user/orderlist").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderDetail orderDetail=new OrderDetail();
        request.setAttribute("orderDetail",orderDetail);
     doPost(request, response);
    }
}
